Page({
    data: {
        id : '' ,
        form : {
            avatarUrl : '' ,
            real_name : '' ,
            more : '' ,
        }
    },

    onLoad: function (opt) {
        this.setData({
            [`form.real_name`] : opt.name ,
            [`form.avatarUrl`] : opt.ava ,
            [`form.more`] : opt.more,
            id : opt.id
        })
    },

    upload: function (e) {
        let that = this
        let id = that.data.id
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: function (res) {
                wx.showLoading({
                    title: '上传中',
                })
                wx.cloud.uploadFile({
                    cloudPath: 'profile/' + id + "/" + new Date().getTime() + '.png',
                    filePath: res.tempFilePaths[0]
                }).then(xRes => {
                    wx.cloud.getTempFileURL({
                        fileList: [xRes.fileID],
                        success: xDoRes => {
                            let path = xDoRes.fileList[0].tempFileURL
                            that.setData({
                                [`form.avatarUrl`] : path
                            })
                            wx.hideLoading()
                        }
                    })
                })
            }
        })
    },

    xInput : function (e) {
        let that = this
        let value = e.detail.value
        let key = e.currentTarget.dataset.key
        that.setData({
            [`form.${key}`]: value
        })
    },

    xSub : function () {
        let that = this
        let form = that.data.form
        for(var i in form){
            if(form[i]==='' || form[i]===undefined){
                wx.showToast({
                    title: '信息不完善',
                })
                return false
            }
        }
        wx.cloud.callFunction({
            name: "user",
            data: {
                action: "edit",
                formdata: form
            }
        }).then(
            wx.showToast({
                title: '提交成功',
                success: () => {
                    setTimeout(() => {
                        wx.navigateBack({
                            delta: 1,
                        })
                    }, 1000);
                }
            })
        )
    }
})